﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
using MvcPaging;
namespace zichan.BLL
{
   public class MachineRoomService
    {
        public static int MaxSort()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.MachineRooms.Count() > 0)
                    return db.MachineRooms.Max(o => o.Sort);
                else
                    return 0;
            }
        }
        public static string Add(MachineRoom model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try {
                    if (db.MachineRooms.Count(o => !o.IsDelete.HasValue && o.Name == model.Name && o.MachineRoomClassId == model.MachineRoomClassId) > 0)
                        return "该机房已存在";
                    db.MachineRooms.Add(model);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch(Exception ex)
                {
                    return "无法添加";
                }
            }
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="nameList">输入的名称集合</param>
        /// <param name="existName">重复的名称</param>
        /// <param name="nameListForLog">保存入数据库的名称，用以做日志使用</param>
        /// <param name="classId">类型id</param>
        /// <returns></returns>
        public static string AddBatch(List<string> nameList, out List<string> existName, out List<string> nameListForLog,int classId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    int maxSort = db.MachineRooms.Max(o => o.Sort);
                    List<MachineRoom> List = new List<MachineRoom>();
                    foreach (string n in nameList)
                    {
                        if (db.MachineRooms.Count(o =>!o.IsDelete.HasValue&&o.Name == n) > 0 || List.Count(o => o.Name == n) > 0)
                        {
                            existName.Add(n);
                            continue;
                        }
                        nameListForLog.Add(n);
                        MachineRoom model = new MachineRoom();
                        maxSort += 1;
                        model.Name = n;
                        model.Sort = maxSort;
                        model.MachineRoomClassId = classId;
                        List.Add(model);
                    }
                    if (List.Count() > 0)
                    {
                        db.MachineRooms.AddRange(List);
                        db.SaveChanges();
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    return "无法添加，请重试";
                }
            }
        }
        public static MachineRoom Get(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
               return db.MachineRooms.Where(o => o.Id == id).FirstOrDefault();
            }
        }
        public static string Edit(MachineRoom model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    if (db.MachineRooms.Count(o => !o.IsDelete.HasValue && o.MachineRoomClassId == model.MachineRoomClassId && o.Name == model.Name&&model.Id!=o.Id) > 0)
                        return "该机房已存在";
                    db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return string.Empty;
                }
                catch(Exception ex)
                {
                    return "无法编辑";
                }
            }
        }
        public static string Delete(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    if (db.Cabinets.Count(o => o.MachineRoomId == id) > 0 || db.IPs.Count(o => o.MachineRoomId == id) > 0 || db.PutawayServers.Count(o => o.MachineRoomId == id) > 0)
                        return "该机房正在使用中，无法删除";
                    MachineRoom model = db.MachineRooms.FirstOrDefault(o => o.Id == id);
                    model.IsDelete = 1;
                    //db.MachineRooms.Remove(model);
                    db.SaveChanges();
                    MachineRoomLogService.Add(1, "删除" + "\"" + model.Name + "\"");
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "无法删除";
                }
            }
        }
        public static IPagedList<MachineRoom> List(string name,int? classId,int pageIndex,int pageSize)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<MachineRoom> m = db.MachineRooms.Where(o=> !o.IsDelete.HasValue);
                if (!string.IsNullOrEmpty(name))
                    m = m.Where(o => o.Name.Contains(name));
                if (classId.HasValue)
                    m = m.Where(o => o.MachineRoomClassId == classId);
                List<MachineRoom> list = m.OrderBy(o=>o.Sort).ThenBy(o=>o.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                List<int> classIds = list.Select(o => o.MachineRoomClassId).ToList();
                var classNames = db.MachineRoomClasses.Where(o => classIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                foreach(MachineRoom r in list)
                {
                    r.MachineRoomClassName = classNames.FirstOrDefault(o => o.Id == r.MachineRoomClassId).Name;
                }
                return list.OrderBy(o => o.Sort).ThenBy(o=>o.Id).ToPagedList(pageIndex - 1, pageSize, m.Count());
            }
        }
        public static List<MachineRoom> List(string name, int? classId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<MachineRoom> m = db.MachineRooms.Where(o => !o.IsDelete.HasValue);
                if (!string.IsNullOrEmpty(name))
                    m = m.Where(o => o.Name.Contains(name));
                if (classId.HasValue)
                    m = m.Where(o => o.MachineRoomClassId == classId);
               
                List<int> classIds = m.Select(o => o.MachineRoomClassId).ToList();
                var classNames = db.MachineRoomClasses.Where(o => classIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                foreach (MachineRoom r in m)
                {
                    r.MachineRoomClassName = classNames.FirstOrDefault(o => o.Id == r.MachineRoomClassId).Name;
                }
                return m.OrderByDescending(o => o.Id).ToList();
            }
        }
        public static List<MachineRoom> List()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.MachineRooms.ToList();
            }
        }
    }
}
